package com.neusoft.elm.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.neusoft.elm.dao.BusinessDao;
import com.neusoft.elm.po.Business;
import com.neusoft.elm.util.DBUtil;

public class BusinessDaoImpl implements BusinessDao {
    private Connection con = null;
    private PreparedStatement pst = null;
    private ResultSet rs = null;

    @Override
    public List<Business> listBusiness() throws Exception {
        List<Business> list = new ArrayList<>();
        String sql = "SELECT * FROM business ORDER BY businessId";
        
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            rs = pst.executeQuery();
            
            while (rs.next()) {
                Business business = createBusinessFromResultSet(rs);
                list.add(business);
            }
        } finally {
            DBUtil.close(rs, pst);
        }
        
        return list;
    }

    @Override
    public Business getBusinessById(Integer businessId) throws Exception {
        Business business = null;
        String sql = "SELECT * FROM business WHERE businessId = ?";
        
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1, businessId);
            rs = pst.executeQuery();
            
            if (rs.next()) {
                business = createBusinessFromResultSet(rs);
            }
        } finally {
            DBUtil.close(rs, pst);
        }
        
        return business;
    }

    @Override
    public List<Business> listBusinessBySort(String sortType) throws Exception {
        List<Business> list = new ArrayList<>();
        String sql = "SELECT * FROM business ORDER BY ";
        
        // 根据排序类型构建 SQL
        switch (sortType) {
            case "distance":
                sql += "distance ASC"; // 距离从近到远
                break;
            case "sales":
                sql += "monthlySales DESC"; // 销量从高到低
                break;
            case "rating":
                sql += "rating DESC"; // 评分从高到低
                break;
            default:
                sql += "businessId ASC"; // 默认按ID排序
        }
        
        System.out.println("执行排序查询SQL: " + sql);
        
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            rs = pst.executeQuery();
            
            while (rs.next()) {
                Business business = createBusinessFromResultSet(rs);
                list.add(business);
                System.out.println("查询到商家: " + business.getBusinessName() + 
                                 ", 距离: " + business.getDistance() + 
                                 ", 销量: " + business.getMonthlySales() + 
                                 ", 评分: " + business.getRating());
            }
        } finally {
            DBUtil.close(rs, pst);
        }
        
        return list;
    }
    
    /**
     * 从 ResultSet 创建 Business 对象
     */
    private Business createBusinessFromResultSet(ResultSet rs) throws Exception {
        Business business = new Business();
        business.setBusinessId(rs.getInt("businessId"));
        business.setPassword(rs.getString("password"));
        business.setBusinessName(rs.getString("businessName"));
        business.setBusinessAddress(rs.getString("businessAddress"));
        business.setBusinessExplain(rs.getString("businessExplain"));
        business.setStarPrice(rs.getDouble("starPrice"));
        business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
        
        // 设置新增字段
        business.setDistance(rs.getDouble("distance"));
        business.setMonthlySales(rs.getInt("monthlySales"));
        business.setRating(rs.getDouble("rating"));
        
        return business;
    }
}